package bsc.war.test;

import bsc.ejb.entity.Map;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.metadata.Comparation;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.model.Score;
import bsc.war.service.ScoreCardUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Memo
 */
public class MapListScoreCards extends AbstractController {

    public MapListScoreCards() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            processRequestInternal(request, response, delegate);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Catched exception", e);
        }
        return new ModelAndView("mapListScoreCards");
    }

    public static void processRequestInternal(HttpServletRequest request,
            HttpServletResponse response, DelegateBSC delegate) {

        HttpSession session = request.getSession();
        Date date = (Date)session.getAttribute("date");

        List<Map> listMap = delegate.findMap();
        
        java.util.Map<ScoreCard, Double> scoreCardScore = new HashMap<ScoreCard, Double>();
        for (Map map : listMap) {
            ScoreCard scoreCard = delegate.findScoreCard(map.getIdScoreCard());
            Comparation comparation = delegate.findComparation(2L);
            Score score = ScoreCardUtils.calculateScore(delegate, comparation, scoreCard, date);
            scoreCardScore.put(scoreCard, score.getScore());

        }




        //System.out.println("Saque la lista con tamaño: " + scoreCardsRoot.size());
        request.setAttribute("listMap", listMap);
        request.setAttribute("scoreCardScore", scoreCardScore);

    }
}
